//商品上架
$(function(){
	//搜索
	$('.searchBar').click(function(){
		var thisform=$(this).parent('form');
		var goods_no=thisform.find('input[name=goods_no]').val();
		var goods_name=thisform.find('input[name=goods_name]').val();
		var bar_code=thisform.find('input[name=bar_code]').val();
		var start_time=thisform.find('input[name=start_time]').val();
		var end_time=thisform.find('input[name=end_time]').val();
		$('#goodsDatagrid').datagrid('options').pageNumber=1;//设置页码初始值为1
		$('#goodsDatagrid').datagrid({ 
			queryParams:{
				goods_no:goods_no,
				goods_name:goods_name,
				start_time:start_time,
				end_time:end_time,
				bar_code:bar_code
			}
		});
	})

	$('#goodsDatagrid').datagrid({
		url:getgoodslistUrl,
		title:'商品管理',
		fitColumns:true,
		rownumbers:true,
		pageSize:25,
		pageNumber:1,
		pageList:[5,10,15,20,25,30,40,50],
		columns:[[
		{field:'id',title:'id',checkbox:true},
		{field:'goods_no',title:'商品编码',sortable:true},
		{field:'manua_id',title:'账册编号',sortable:true},
		{field:'goods_name',title:'商品名称',sortable:true},
		{field:'bar_code',title:'条形码',sortable:true},
		{field:'has_grounding',title:'总实收已上架',sortable:true},
		{field:'no_grounding',title:'总实收未上架',sortable:true},
		{field:'down_grounding',title:'总实收已下架',sortable:true},
		{field:'total_inbound_nums',title:'总导单',sortable:true},
		{field:'total_oubound_nums',title:'总出库',sortable:true},
		{field:'total_receive_nums',title:'总接收',sortable:true},
		{field:'total_delete_nums',title:'总删单',sortable:true},
		{field:'total_hz_nums',title:'总盘盈核增',sortable:true},
		{field:'total_hj_nums',title:'总盘亏核减',sortable:true},
		{field:'xx',title:'理论剩余库存',
			formatter:function(val,rowData,rowIndex){
				// 理论剩余库存=总实收上架 - 总导单;
				return parseInt(rowData.has_grounding)-parseInt(rowData.total_inbound_nums);
				// return rowData.has_grounding-rowData.total_inbound_nums;
		    }
		},
		{field:'storage_amount',title:'实际剩余库存',sortable:true},
		{field:'yy',title:'差额',
			formatter:function(val,rowData,rowIndex){
				// 差额=理论剩余库存 - 实际剩余库存;
				//         return rowData.has_grounding+rowData.total_hz_nums-rowData.total_hj_nums-rowData.total_inbound_nums-rowData.storage_amount;
				return parseInt(rowData.has_grounding)-parseInt(rowData.total_inbound_nums)-parseInt(rowData.storage_amount);
		    }
		},
		// 剩余未出库=剩余冻结
		{field:'total_oubound_nums_no',title:'剩余未出库',sortable:true},
		{field:'freez_nums',title:'剩余冻结',sortable:true},
		{field:'inbound_nums',title:'当日导单',sortable:true},
		{field:'receive_nums',title:'当日接收',sortable:true},
		{field:'oubound_nums',title:'当日出库',sortable:true},
		{field:'pack_nums',title:'当日打包',sortable:true},
		{field:'weight_nums',title:'当日称重',sortable:true},
		{field:'delete_order_nums',title:'当日删单',sortable:true},
		{field:'hz_nums',title:'当日盘盈核增',sortable:true},
		{field:'hj_nums',title:'当日盘亏核减',sortable:true},
		{field:'add_time',title:'添加时间',sortable:true},
		{field:'cz',title:'操作',
			formatter:function(val,rowData,rowIndex){
		        if(val=="需盘库"){
		            return "<font color='red'>"+val+"<font>";
				}
		    }
		}
		]],
		onLoadError:function(data){
			var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				var tab=$('#Tabs').tabs('getSelected');
				var index=$('#Tabs').tabs('getTabIndex',tab);
				$('#Tabs').tabs('close',index);
			});
		},
		rowStyler:function(index,row){
			if(row.has_grounding===null){
				has_grounding=0;
			}else{
				has_grounding=row.has_grounding;
			}
			if(row.total_oubound_nums===null){
				total_oubound_nums=0;
			}else{
				total_oubound_nums=row.total_oubound_nums;
			}
			if(row.storage_amount===null){
				storage_amount=0;
			}else{
				storage_amount=row.storage_amount;
			}
			if(row.freez_nums===null){
				freez_nums=0;
			}else{
				freez_nums=row.freez_nums;
			}
			// console.log(has_grounding-total_oubound_nums);
			// console.log(storage_amount+freez_nums);
			//差额不为0的背景为黄色
			if(parseInt(row.has_grounding)-parseInt(row.total_inbound_nums) != parseInt(row.storage_amount)){
				return 'background-color:yellow;color:blue;font-weight:bold;';
			}
			if (row.color==0){
				return 'background-color:pink;color:blue;font-weight:bold;';
			}
		},
		pagination:true,
		fit:true,
	});
});
goodsObj={
	showAll:function(){
		$('#goodsDatagrid').datagrid({ queryParams:''});
	},
	excelBar:function(){	//导出excel
		var thisform=$('#goodsSearchForm');
		var goods_no=thisform.find('input[name=goods_no]').val();
		var goods_name=thisform.find('input[name=goods_name]').val();
		var bar_code=thisform.find('input[name=bar_code]').val();
		var start_time=thisform.find('input[name=start_time]').val();
		var end_time=thisform.find('input[name=end_time]').val();
		url=getgoodslistUrl+"&excel=1&rows=6000";
		if(goods_no!=''){
			url=url+"&goods_no="+goods_no;
		}
		if(goods_name!=''){
			url=url+"&goods_name="+goods_name;
		}
		if(bar_code!=''){
			url=url+"&bar_code="+bar_code;
		}
		url=url+"&start_time="+start_time+"&end_time="+end_time;
		window.location.href=url;
	}
}